package com.metadata_system.api_server.system_server;

import com.metadata_system.api.annotations.CheckTokenUser;
import com.metadata_system.common.db.entity.Metadata;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.http.request_dto.LoginUserRequest;
import com.metadata_system.system.request.MetadataListParams;
import com.metadata_system.system.request.MetadataNodeListParams;
import com.metadata_system.system.service.MetadataEdgeService;
import com.metadata_system.system.service.MetadataNodeService;
import com.metadata_system.system.service.MetadataService;
import org.bouncycastle.asn1.cms.MetaData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

@RestController
@RequestMapping("/api/system-server/v001/Metadata")
public class MetadataController {
    @Autowired
    private MetadataService metadataService;
    @Autowired
    LoginUserRequest loginUserRequest;

    @RequestMapping("getList")
    @CheckTokenUser
    public ApiResult getList(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "createdTime", required = false) String createdTime,
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "sortProp", required = false) String sortProp,
            @RequestParam(value = "sortOrder", required = false) String sortOrder
    ) {
        return metadataService.getList(MetadataListParams.builder()
                .name(name)
                .createdTime(createdTime)
                .page(page)
                .pageSize(pageSize)
                .sortProp(sortProp)
                .sortOrder(sortOrder)
                .build());
    }

    @RequestMapping("getNodeList")
    @CheckTokenUser
    public ApiResult getNodeList(
            @RequestParam(value = "id", required = false) Integer id
    ) {
        return metadataService.getNodeList(id);
    }

    @RequestMapping("getDetail")
    @CheckTokenUser
    public ApiResult getDetail(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "viewId", required = false) Integer viewId
    ) {
        return metadataService.getDetail(id);
    }

    @RequestMapping("create")
    @CheckTokenUser
    @Transactional
    public ApiResult create(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "shortName", required = false) String shortName
    ) {
        ApiResult apiResult = metadataService.create(Metadata.builder()
                .name(name)
                .shortName(shortName)
                .createdUserId(loginUserRequest.getTokenUser().decodeID())
                .build(),loginUserRequest.getTokenUser().decodeID());
        if (apiResult.getCode() != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return apiResult;
    }

    @RequestMapping("update")
    @CheckTokenUser
    @Transactional
    public ApiResult update(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "shortName", required = false) String shortName,
            @RequestParam(value = "enableEdgesArr", required = false) String enableEdgesArr
    ) {
        ApiResult apiResult = metadataService.update(Metadata.builder()
                .id(id)
                .name(name)
                .shortName(shortName)
                .enableEdgeArr(enableEdgesArr)
                .build(),loginUserRequest.getTokenUser().decodeID());
        if (apiResult.getCode() != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return apiResult;
    }

    @RequestMapping("delete")
    @CheckTokenUser
    @Transactional
    public ApiResult delete(
            @RequestParam(value = "id", required = false) Integer id
    ) {
        ApiResult apiResult = metadataService.delete(id,loginUserRequest.getTokenUser().decodeID());
        if (apiResult.getCode() != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return apiResult;
    }


    @RequestMapping("getAllList")
    @CheckTokenUser
    @Transactional
    public ApiResult getAllList(
    ) {
        ApiResult apiResult = metadataService.getAllList();
        if (apiResult.getCode() != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return apiResult;
    }
}
